<?php
namespace App\Repositories\Eloquent\Eticket;

use App\Models\Eticket\Ticket;
use App\Repositories\Contracts\Eticket\TicketRepository;
use Prettus\Repository\Eloquent\BaseRepository;
use Prettus\Repository\Criteria\RequestCriteria;

/**
 * Class ActivityRepositoryEloquent
 * @package namespace App\Repositories\Eloquent;
 */
class TicketRepositoryEloquent extends BaseRepository implements TicketRepository
{
    /**
     * Specify Model class name
     *
     * @return string
     */
    public function model()
    {
        return Ticket::class;
    }

    /**
     * 查询权限并分页
     * @author 晚黎
     * @date   2016-11-02T15:17:24+0800
     * @param  [type]                   $start  [起始数目]
     * @param  [type]                   $length [读取条数]
     * @param  [type]                   $search [搜索数组数据]
     * @param  [type]                   $order  [排序数组数据]
     * @return [type]                           [查询结果集，包含查询的数量及查询的结果对象]
     */
    public function getTicketList($start, $length, $search, $order, $type = '', $way = null)
    {
        $ticket = $this->model;
        if (!empty($type)) {
            $ticket = $ticket->where('type', $type);
        }
        if ($way!==null) {
            $ticket = $ticket->where('way', intval($way));
        }
        if ($search['value']) {
            if ($search['regex'] == 'true') {
                $ticket = $ticket->where('number', 'like', "%{$search['value']}%");
            } else {
                $ticket = $ticket->where('number', $search['value']);
            }
        }

        $count = $ticket->count();

        $ticket = $ticket->orderBy($order['name'], $order['dir']);

        $tickets = $ticket->offset($start)->limit($length)->get();

        return compact('count', 'tickets');
    }

    /**
     * 获取所有的权限并按照功能分组
     * @author 晚黎
     * @date   2016-11-03T13:20:18+0800
     * @return [type]                   [description]
     */
    public function groupTicketList()
    {
        $tickets = $this->model->all();
        $array = [];
        if ($tickets) {
            foreach ($tickets as $v) {
                array_set($array, $v->slug, ['id' => $v->id, 'name' => $v->name]);
            }
        }
        return $array;
    }

}
